<template>
  <div>
    <h2>会员添加</h2>
    <hr />
    <table>
      <tr>
        <td>会员编号:</td>
        <td>
          <input type="text" v-model="state.kh" />
        </td>
      </tr>
      <tr>
        <td>手机号:</td>
        <td>
          <input type="text" v-model="state.phone" />
        </td>
      </tr>
      <tr>
        <td>姓名:</td>
        <td>
          <input type="text" v-model="state.name" />
        </td>
      </tr>
      <tr>
        <td>证件类型:</td>
        <td>
          
          <select v-model="state.type">
            <option value="请选择">请选择</option>
            <option value="身份证">身份证</option>
            <option value="户口本">户口本</option>
          </select>
        </td>
      </tr>
      <tr>
        <td>证件号:</td>
        <td>
          <input type="text" v-model="state.sfz" />
        </td>
      </tr>
      <tr>
        <td>地址:</td>
        <td>
          <input type="text" v-model="state.address" />
        </td>
      </tr>
      <tr>
        <td>民族:</td>
        <td>
          <input type="text" v-model="state.mz" />
        </td>
      </tr>
      <tr>
        <td>会员等级:</td>
        <td>
          <select v-model="state.dj">
            <option value="请选择">请选择</option>
            <option value="普通会员">普通会员</option>
            <option value="白金会员">白金会员</option>
            <option value="黄金会员">黄金会员</option>
          </select>
        </td>
      </tr>
      <tr>
        <td>生日:</td>
        <td>
          <input type="date" v-model="state.sr" />
        </td>
      </tr>
      <tr>
        <td>性别:</td>
        <td>
          <input type="radio" v-model="state.sex"/>男
          <input type="radio" v-model="state.sex"/>女
        </td>
      </tr>
      <tr>
        <td>
          <input type="button" value="添加" @click="MemberAdd" />
        </td>
        <td>
          <input type="button" value="会员列表" @click="MemberShow" />
        </td>
      </tr>
    </table>
  </div>
</template>

<script setup lang="ts">
import { reactive, toRefs, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import axios from 'axios';

const router = useRouter();
const route = useRoute();

let data: any = reactive({
  state: {
    kh: '',
    phone: '',
    name: '',
    type: '',
    sfz: '',
    address: '',
    mz: '',
    dj: '',
    sr: '',
    sex: '',
  },
});
let { state } = toRefs(data);

function MemberAdd() {
  let obj = {

    kh:state.value.kh,
    phone:state.value.phone,
    name:state.value.name,
    type:state.value.type,
    sfz:state.value.sfz,
    address:state.value.address,
    mz: state.value.mz,
    dj:state.value.dj,
    sr:state.value.sr,
    sex:state.value.sex,
    cTime:new Date,
    jf: 100,
  };
  if(obj.kh=="")
  {
    alert('会员编号不能为空')
    return
  }
  if(obj.phone=="")
  {
    alert('手机号不能为空')
    return
  }
  if(obj.name=="")
  {
    alert('姓名不能为空')
    return
  }
  if(obj.type=="")
  {
    alert('会员类型不能为空')
    return
  }
  if(obj.sfz=="")
  {
    alert('证件号不能为空')
    return
  }
  if(obj.sfz.length!=18)
  {
    alert('证件号必须是18位')
    return 
  }
  if(obj.address=="")
  {
    alert('地址不能为空')
    return
  }
  if(obj.mz=="")
  {
    alert('民族不能为空')
    return
  }
  if(obj.dj=="")
  {
    alert('等级不能为空')
    return
  }
 
  axios({
    url:'https://localhost:7278/api/Member/MemberAdd',
    method:'post',
    data:obj
  })
  .then(res=>{
    if(res.data.data>0)
  {
    alert('添加成功')
    router.push('/MemberShow')
  }
  else{
    alert('添加失败')
    return 
  }
  })
  .catch(err=>{
    console.log(err)
  })


}


function MemberShow(){
  router.push('/MemberShow')
}

</script>

<style scoped></style>
